<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<style>
    <!--
    -->
    .form-group > label {
        width: 60px;
    }
</style>
<script type="text/javascript">
    var menuids =${sessionScope.menus_session_key}+",";
    var email = {
        searchFormCallback: function (json) {
            console.log(json);
            if (json.code == 0) {
                var data = json.data;
                if (data.length < 1) {
                    $(this).alertmsg('info', "无数据");
                }
                email.showGridData(data, 'O');
            } else {
                $(this).alertmsg('error', json.msg);
            }
        },
        showGridData: function (data, domainNo) {
            var list = data;
            var trs = "";

            var tempCount = '<tr><td>{tp0}</td><td>{tp1}</td><td>{tp2}</td><td>{tp3}</td><td>{tp4}</td></tr>';

            for (var i = 0; list != null && i < list.length; i++) {
                var operation = '';
                if (menuids.indexOf(",39,") > -1) {
                    operation = operation + '<a class="btn btn-blue" data-icon="info" data-id="cpi"	data-title="查看邮件信息" data-height="800" data-width="850" data-mask="true" data-toggle="dialog" href="email/detail/' + list[i].id + '.shtml">详情</a>';
                }
                if (menuids.indexOf(",40,") > -1) {
                    operation = operation + '&nbsp;<a class="btn btn-blue" data-icon="edit" data-id="cpi"	data-title="编辑邮件信息" data-height="800" data-width="850" data-mask="true" data-toggle="dialog" href="email/page/update.shtml?id=' + list[i].id + '">编辑</a>';
                }
                if (menuids.indexOf(",41,") > -1) {
                    operation = operation + '&nbsp;<a class="btn btn-blue" data-icon="edit" data-id="cpi"	data-title="选择邮件接收人" data-height="500" data-width="400" data-mask="true" data-toggle="dialog" href="email/chooseReceiverPage/' + list[i].id + '.shtml">发送</a>';
                }
                if (menuids.indexOf(",42,") > -1) {
                    operation = operation + '&nbsp;<a	class="btn btn-red" data-icon="minus" data-mask="true" data-toggle="doajax" data-confirm-msg="请确认是否删除当前邮件？" data-callback="BackForThis"  href="email/delete/' + list[i].id + '.shtml">删除</a>';
                }


                var tempStr = tempCount
                    .replace("{tp0}", (i + 1))
                    .replace("{tp1}", list[i].name)
                    .replace("{tp3}", list[i].create_time)
                    .replace("{tp4}", operation);

                if ($.CurrentNavtab.find("#emailType option:selected").val() == "0") {
                    tempStr = tempStr
                        .replace("{tp2}", list[i].state == 0 ? "<span style='color: red'>未读</span>" : "<span style='color: green'>已读</span>")
                    ;
                } else {
                    tempStr = tempStr
                        .replace("{tp2}", list[i].state == 0 ? "<span style='color: red'>未发</span>" : "<span style='color: green'>已发</span>")
                    ;
                }

                trs += tempStr;

                $.CurrentNavtab.find("#emaillist").html("");
                $.CurrentNavtab.find("#emaillist").append(trs);

            }

            $.CurrentNavtab.find("th").css("text-align", "center");
            $.CurrentNavtab.find("td").css("text-align", "center");
        }
    };

    function BackForThis(json) {
        var type = $.CurrentNavtab.find("#emailType option:selected").val();
        if (type == '0') {
            Dracula.post('email/inmails.shtml', "", email.searchFormCallback);
        } else {
            Dracula.post('email/outmails.shtml', "", email.searchFormCallback);
        }
    }

    $(function () {

        var emailList = ${emails};
        email.showGridData(emailList, 'O');

        $.CurrentNavtab.find("#emailType").on("change", function () {
            var type = $.CurrentNavtab.find("#emailType option:selected").val();
            if (type == '0') {
                Dracula.post('email/inmails.shtml', "", email.searchFormCallback);
            } else {
                Dracula.post('email/outmails.shtml', "", email.searchFormCallback);
            }
        });
    });

</script>

<%--查询表单--%>
<div class="bjui-pageHeader"
     style="height: auto; padding-top: 10px; padding-bottom: 10px;">
    <form id="pagerForm" class="pageForm" data-toggle="ajaxsearch" method="post">
        <div class="bjui-searchBar">
            <div class="row">
                <div class="col-md-4">
                    <div class="form-group">
                        <label>信箱：</label>
                        <select name="type" id="emailType" data-width="200"
                                class="form-control" data-toggle="selectpicker" data-size="10">
                            <option value="0" selected>收件箱</option>
                            <option value="1">发件箱</option>
                        </select>
                    </div>
                </div>
                <c:if test="${fn:contains(sessionScope.menus_session_key,',38,')}">
                    <div class="col-md-8">
                        <div class="form-group " align="center">
                            <a class="btn btn-blue" data-icon="plus" data-id="addtask"
                               data-title="创建新邮件" data-height="800" data-width="850"
                               data-mask="true" data-toggle="dialog" data-resizable=false data-maxable=false
                               href="email/page/add.shtml">创建新邮件</a>

                        </div>
                    </div>
                </c:if>
            </div>
        </div>
    </form>
</div>

<%--信息表格--%>
<div class="bjui-pageContent tableContent" id="contentList">
    <table data-toggle="tablefixed" data-width="100%" data-nowrap="true" id="emailtable">
        <thead>
        <tr id="emailtr">
            <th width="40">编号 <br></th>
            <th width="120">邮件名称<br></th>
            <th width="120">状态<br></th>
            <th width="120">创建时间<br></th>
            <th width="200">操作<br></th>
        </tr>
        </thead>
        <tbody id="emaillist">
        </tbody>
    </table>
</div>
